//
//  CCQNetworkTools.swift
//  swoft
//
//  Created by 神威 on 16/9/21.
//  Copyright © 2016年 ccq. All rights reserved.
//

import UIKit
import AFNetworking
//  网络请求类型
enum RequestType: Int {
    // get请求
    case GET
    // post请求
    case POST
}
class CCQNetworkTools: AFHTTPSessionManager {
    //单例全局访问点
    static let sharedTools:CCQNetworkTools = {
        let tools = CCQNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    //  封装get/post请求
   fileprivate func request(type: RequestType, url: String, params: Any?, callBack: @escaping (Any?, Error?)->()) {
        
        if type == .GET {
            get(url, parameters: params, progress: nil, success: { (_, response) in
                callBack(response, nil)
                }, failure: { (_, error) in
                    callBack(nil, error)
            })
            
        } else {
            post(url, parameters: params, progress: nil, success: { (_, response) in
                callBack(response, nil)
                }, failure: { (_, error) in
                    callBack(nil, error)
            })
            
        }
        
  }
    


}

//  MARK: -- 微博首页相关接口
extension CCQNetworkTools {
    func requestStatuses(accessToken: String, maxId: Int64, sinceId: Int64, callBack: @escaping (Any?, Error?)->()) {
        //  准备接口地址
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        //  准备参数
        let params: [String: Any] = [
            "access_token": accessToken,
            "max_id": maxId,
            "since_id": sinceId
        ]
        
        print("----------")
        print(url + "?access_token=\(accessToken)")
        print("----------")
        
        //  执行接口请求
        request(type: .GET, url: url, params: params, callBack: callBack)
    }
    
}


///OAuth登录相关的接口
extension CCQNetworkTools{
    
    //通过AccesstoKen获取用户信息
    func requestUserInfo(accessToken:String,uid:Int64,callBack: @escaping (Any?, Error?) -> ()) -> Void {
        //请求接口地址
        let url = "https://api.weibo.com/2/users/show.json"
        
        //准备请求需要的参数
        let params :[String : Any] = [
            
            "access_token" : accessToken,
            
            "uid" : uid

        ]
        
        request(type: .GET, url: url, params: params, callBack: callBack)
    }
    
    //根据code获取accesstoken
    func requestAccessToken(code:String,callBack:@escaping (Any?, Error?)->()){
        //请求接口
        let url = "https://api.weibo.com/oauth2/access_token"
        // 准备请求需要的
        let params = [
            
            "client_id" : WeiboAppKey,
            "client_secret" : WeiboAppSecret,
            "grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri" : WeiboRedirect_Uri
        ]
        
        request(type: .POST, url: url, params: params, callBack: callBack)
    }
}



